package com.michaelbulava.TFS.ui.config;

import com.michaelbulava.TFS.ui.dialogs.connect.CredentialsCompleteDialog;
import com.microsoft.tfs.core.httpclient.Credentials;
import com.microsoft.tfs.core.ws.runtime.exceptions.FederatedAuthException;
import com.microsoft.tfs.core.ws.runtime.exceptions.UnauthorizedException;
import com.microsoft.tfs.util.Check;

import java.net.URI;

/**
 * Created by mbulava on 5/6/2015.
 */
public class UITransportFederatedFallbackAuthRunnable extends UITransportAuthRunnable {
    private final URI serverURI;
    private final Credentials credentials;
    private final FederatedAuthException exception;

    public UITransportFederatedFallbackAuthRunnable(URI serverURI, Credentials credentials, FederatedAuthException exception) {
        Check.notNull(serverURI, "serverURI");
        Check.notNull(credentials, "credentials");
        Check.notNull(exception, "exception");

        this.serverURI = serverURI;
        this.credentials = credentials;
        this.exception = exception;
    }

    @Override
    protected CredentialsCompleteDialog getCredentialsDialog() {
        UITransportFederatedAuthRunnable subRunnable = new UITransportFederatedAuthRunnable(this.serverURI, this.exception);
        if (subRunnable.isAvailable())
            return subRunnable.getCredentialsDialog();

        UnauthorizedException unauthorizedException = new UnauthorizedException(this.exception.getServerURI(), this.credentials);
        return new UITransportUsernamePasswordAuthRunnable(this.serverURI, this.credentials, unauthorizedException).getCredentialsDialog();
    }
}
